<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>账单添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8"/>
    <link rel="stylesheet" href="/static/css/font.css">
    <link rel="stylesheet" href="/static/css/xadmin.css">
    <script type="text/javascript" src="/static/js/jquery.min.js"></script>
    <script type="text/javascript" src="/static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/js/xadmin.js"></script>
    <script type="text/javascript" src="/static/js/index.js"></script>
    <style>
        .layui-form {
            padding: 20px 50px;
        }
    </style>
    <script type="text/javascript">
        function sum(obj) {
            var basewage = document.getElementById("basewage");
            var gradewage = document.getElementById("gradewage");
            var performance = document.getElementById("performance");
            var otherwage = document.getElementById("otherwage");
            var fiveinsurance = document.getElementById("fiveinsurance");
            var tax = document.getElementById("tax");
            var otherpayment = document.getElementById("otherpayment");
            var sumwage = document.getElementById("sumwage");
            if (basewage.value != '') {
                sumwage.value = parseFloat(basewage.value);
            }
            if (basewage.value != '' && gradewage.value != '') {
                sumwage.value = parseFloat(basewage.value) + parseFloat(gradewage.value);
            }
            if (basewage.value != '' && gradewage.value != '' && performance.value != '') {
                sumwage.value = parseFloat(basewage.value) + parseFloat(gradewage.value) + parseFloat(performance.value);
            }
            if (basewage.value != '' && gradewage.value != '' && performance.value != '' && otherwage.value != '') {
                sumwage.value = parseFloat(basewage.value) + parseFloat(gradewage.value) + parseFloat(performance.value) + parseFloat(otherwage.value);
            }
            if (basewage.value != '' && gradewage.value != '' && performance.value != '' && otherwage.value != '' && fiveinsurance.value != '') {
                sumwage.value = parseFloat(basewage.value) + parseFloat(gradewage.value) + parseFloat(performance.value) + parseFloat(otherwage.value) - parseFloat(fiveinsurance.value);
            }
            if (basewage.value != '' && gradewage.value != '' && performance.value != '' && otherwage.value != '' && fiveinsurance.value != '' && tax.value != '') {
                sumwage.value = parseFloat(basewage.value) + parseFloat(gradewage.value) + parseFloat(performance.value) + parseFloat(otherwage.value) - parseFloat(fiveinsurance.value) - parseFloat(tax.value);
            }
            if (basewage.value != '' && gradewage.value != '' && performance.value != '' && otherwage.value != '' && fiveinsurance.value != '' && tax.value != '' && otherpayment.value != '') {
                sumwage.value = parseFloat(basewage.value) + parseFloat(gradewage.value) + parseFloat(performance.value) + parseFloat(otherwage.value) - parseFloat(fiveinsurance.value) - parseFloat(tax.value) - parseFloat(otherpayment.value);
            }

        }
    </script>
</head>

<body>
<form class="layui-form layui-form-pane">
    <div class="layui-form-item" lay-filter="bill-form">
        <label class="layui-form-label">基本工资</label>
        <div class="layui-input-block">
            <input type="text" name="basewage" id="basewage" lay-verify="required|number" lay-verType="tips"
                   placeholder="请输入基本工资" autocomplete="off" class="layui-input" onkeyup="sum(this);">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">薪级工资</label>
        <div class="layui-input-block">
            <input type="text" name="gradewage" id="gradewage" lay-verify="required|number" lay-verType="tips"
                   placeholder="请输入薪级工资" autocomplete="off" class="layui-input" onkeyup="sum(this);">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">绩效奖金</label>
        <div class="layui-input-block">
            <input type="text" name="performance" id="performance" lay-verify="required|number" lay-verType="tips"
                   placeholder="请输入绩效奖金" autocomplete="off" class="layui-input" onkeyup="sum(this);">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">其余补贴</label>
        <div class="layui-input-block">
            <input type="text" name="otherwage" id="otherwage" lay-verify="required|number" lay-verType="tips"
                   placeholder="请输入其余补贴" autocomplete="off" class="layui-input" onkeyup="sum(this);">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">五险一金扣款</label>
        <div class="layui-input-block">
            <input type="text" name="fiveinsurance" id="fiveinsurance" lay-verify="required|number" lay-verType="tips"
                   placeholder="请输入五险一金扣款" autocomplete="off" class="layui-input" onkeyup="sum(this);">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">个人所得税</label>
        <div class="layui-input-block">
            <input type="text" name="tax" id="tax" lay-verify="required|number" lay-verType="tips"
                   placeholder="请输入个人所得税" autocomplete="off" class="layui-input" onkeyup="sum(this);">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">其它扣款</label>
        <div class="layui-input-block">
            <input type="text" name="otherpayment" id="otherpayment" lay-verify="required|number" lay-verType="tips"
                   placeholder="请输入其它扣款" autocomplete="off" class="layui-input" onkeyup="sum(this);">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">总工资</label>
        <div class="layui-input-block">
            <input type="text" name="sumwage" id="sumwage" readOnly=readOnly lay-verify="required|number"
                   lay-verType="tips" placeholder="总工资" autocomplete="off" class="layui-input">
        </div>
    </div>
    <input type="hidden" name="id">
    <div class="layui-form-item">
        <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="add" style="display:none;">立即提交</button>
        <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="update" style="display:none; margin-left: 0;">
            立即提交
        </button>
        <button type="reset" id="resetForm" style="display: none;">重置</button>
    </div>
</form>
<script>
    var url = location.href;
    if (url.indexOf("#2") != -1) {
        $("button[lay-filter=update]").css("display", "block");
        var index = getQueryString("index");
        initForm(parent.document.getElementsByTagName("tr")[index]);
    } else {
        if (url.indexOf("#3") != -1) {
            initForm();
        }
        $("button[lay-filter=add]").css("display", "block");
    }

    layui.use(['form', 'layer'], function () {
        $ = layui.jquery;
        var form = layui.form
            , layer = layui.layer;


        //监听提交
        form.on('submit(add)', function (data) {
            console.log(data);
            var type = data.field.cardType;
            data.field.type = type;
            data.field.typename = $("#cardType").find("option:selected").text();
            $.post("/wages/add", data.field, function (res) {
                console.log(data);
                if (res.code == 200) {
                    layer.confirm("添加成功！", {
                        title: false,
                        closeBtn: false,
                        skin: 'layui-layer-molv',
                        btn: ["继续添加", "取消"]
                    }, function (layno) {
                        layer.close(layno);
                        $("#resetForm").click();
                    }, function () {
                        layer.msg(res.msg, {time: 1000}, function () {
                            x_admin_close();
                            if (parent.location.href.indexOf("details.html") != -1) {
                                parent.getWages();
                            } else {
                                parent.document.getElementById("home").src = "welcome";
                            }
                        });
                    });
                } else {
                    layer.msg(res.msg)
                }
            })
            return false; //防止form的action提交
        });

        //监听提交
        form.on('submit(update)', function (data) {
            // console.log(data);
            layer.confirm("确认修改？", {title: false, closeBtn: false, skin: 'layui-layer-molv'}, function (layno) {
                layer.close(layno);
                var type = data.field.cardType;
                data.field.type = type;
                data.field.typename = $("#cardType").find("option:selected").text();
                $.post("/wages/update", data.field, function (res) {
                    if (res.code == 200) {
                        layer.msg(res.msg, {time: 1000}, function () {
                            x_admin_close();
                            parent.getWages();
                        });
                    } else {
                        layer.msg(res.msg)
                    }
                });
            });
            return false;   //防止form的action提交
        });
    });

    function initForm(tr) {
        // console.log(tr);
        if (tr == undefined) {
            return;
        } else {
            $("input[name=basewage]").val($(tr).find(":nth-child(3)").html());
            $("input[name=gradewage]").val($(tr).find(":nth-child(4)").html());
            $("input[name=performance]").val($(tr).find(":nth-child(5)").html());
            $("input[name=otherwage]").val($(tr).find(":nth-child(6)").html());
            $("input[name=fiveinsurance]").val($(tr).find(":nth-child(7)").html());
            $("input[name=tax]").val($(tr).find(":nth-child(8)").html());
            $("input[name=otherpayment]").val($(tr).find(":nth-child(9)").html());
            $("input[name=sumwage]").val($(tr).find(":nth-child(10)").html());
            $("input[name=id]").val($(tr).find(":nth-child(12)").html());
        }
    }
</script>
</body>

</html>
